package com.bkitclub.mobiledevcamp.horseword.customGUI;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.FrameLayout.LayoutParams;

/**
 * only use for imageview with parent frame layout
 * @author silong
 *
 */
public class CustomButton extends ImageView implements OnTouchListener{
	public CustomButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		setOnTouchListener(this);
	}
	public CustomButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		setOnTouchListener(this);
	}
	public CustomButton(Context context){
		super(context);
		setOnTouchListener(this);
	}
	public void initResource(Bitmap normalState){
		setImageBitmap(normalState);
		w = normalState.getWidth();
		h = normalState.getHeight();
	}
	private int w,h;
	public int getResourceWidth(){
		return w;
	}
	public int getResourceHeight(){
		return h;
	}
	private static final int DELTA_POSITION = 10;
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		if(event.getAction() == MotionEvent.ACTION_DOWN){
				setDeltaPosition(DELTA_POSITION);
				return false;
		}
		if(event.getAction() == MotionEvent.ACTION_UP){
				setDeltaPosition(-DELTA_POSITION);
				return false;
        }
        return false;
	}
	private void setDeltaPosition(int yy){
		FrameLayout.LayoutParams frameLayout = (LayoutParams) getLayoutParams();
		frameLayout.topMargin += yy;
		setLayoutParams(frameLayout);
	}
}
